﻿using IR.WPF.Interfaces;
using IR.WPF.UI.ViewModels;
using IR.WPF.ViewModels.LandingViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace IR.WPF.UI.Windows
{
    /// <summary>
    /// Interaction logic for CompositeControlHost.xaml
    /// </summary>
    public partial class CompositeControlHost : Window
    {
        private bool _isModal;
        private ViewModelBase _viewModel;

        public CompositeControlHost()
        {
            InitializeComponent();

            MainWindow tempMainWindow = App.Current.MainWindow as MainWindow;
            if (tempMainWindow.LoggedInUser == null)
                this.Close();
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            _viewModel.DoCleanup();

            if (_viewModel.GetType().Equals(typeof(VMHome)))
            {
                MainWindow tempMainWindow = App.Current.MainWindow as MainWindow;
                tempMainWindow.LoggedInUser = null;
            }

            this.Owner.Show();

            base.OnClosing(e);
        }

        public static void ShowWindow<T>(ViewModelBase viewModel, Window owner) where T : Control, new()
        {
            CompositeControlHost window = new CompositeControlHost();
            // Create the hosted control
            T editableControl = new T();

            window.InitializeHostWindow(editableControl, viewModel);
            window.Owner = owner;
            if (viewModel.WindowTitle != null)
                window.Title = viewModel.WindowTitle;
            window.Show();
        }

        public static bool ShowModal<T>(ViewModelBase viewModel, Window owner) where T : Control, new()
        {
            CompositeControlHost window = new CompositeControlHost();
            T editableControl = new T();

            window.InitializeHostWindow(editableControl, viewModel);
            window.Owner = owner;
            if (viewModel.WindowTitle != null)
                window.Title = viewModel.WindowTitle;
            window._isModal = true;
            return window.ShowDialog() ?? false;
        }

        private void InitializeHostWindow(Control editableControl, ViewModelBase viewModel)
        {
            _viewModel = viewModel;
            // Insert the Control into the Grid
            gridControlHost.Children.Insert(0, editableControl);
            // Set the attached properties of the control to make sure it's in the right position
            editableControl.SetValue(Grid.RowProperty, 0);
            editableControl.SetValue(Grid.ColumnProperty, 0);
            _viewModel.BoundWindow = this;

            DataContext = _viewModel;
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            // If this Window is Modal, then set the DialogResult to false
            if (_isModal)
                DialogResult = false;

            // Close the window, throwing away any changes
            Close();
        }
    }
}
